package com.example.mvpdemo.simple4;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.mvpdemo.R;
import androidx.appcompat.app.AppCompatActivity;

public class Simple4Activity extends AppCompatActivity implements WeatherView_4{
    /**
     * simple4
     * mvp
     * 优化：使用泛型设计
     * 发现问题：
     * 随着Activity、Fragment数量的增加，我出现反复的绑定解绑，这种操作属于代码冗余。
     * 解决办法：
     * Activity进行抽象 + 泛型设计 ——>
     * Simple5中进行优化
     *
     */
    private TextView mWeatherContent;
    private EditText mCitynameEdit;
    String cityname = "北京";
    WeatherPresenter_4 weatherPresenter_4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple4);

        mWeatherContent = findViewById(R.id.weather_content_tv);
        mCitynameEdit = findViewById(R.id.cityname_edit);
        mCitynameEdit.setText(cityname);
    }

    public void getWeatherData(View view) {

        cityname = mCitynameEdit.getText().toString().trim();
        weatherPresenter_4 = new WeatherPresenter_4();
        weatherPresenter_4.attachView(this);
        weatherPresenter_4.weeather(cityname);

    }

    @Override
    public void onWeatherGetSuccess(String result) {
        mWeatherContent.setText("查询结果：\n" + result);
    }

    @Override
    public void onWeatherGetFail(String result) {
        mWeatherContent.setText("查询结果：\n" + result);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (weatherPresenter_4 != null) {
            weatherPresenter_4.detachView();
        }
    }

}